package com.mvvm.store.ui.stockup;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.View;

import com.mvvm.store.R;
import com.mvvm.store.base.BaseActivity;
import com.mvvm.store.bean.StockUpParasBean;
import com.mvvm.store.bean.basebean.StockUpResultBean;
import com.mvvm.store.customview.CustomProgress;
import com.mvvm.store.databinding.ActivityTockUpManagerBinding;
import com.mvvm.store.ui.stockup.adapter.StockUpManagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 备货管理界面 --------接口列表数据
 */
public class StockUpManagerActivity extends BaseActivity<StockUpViewModel, ActivityTockUpManagerBinding> {
    int index;
    private StockUpManagerAdapter stockUpManagerAdapter;
    private int curPage = 1;
    private ArrayList<StockUpResultBean> stockUpInfoBeans = new ArrayList<>();
    private CustomProgress dialog;
    private   StockUpParasBean stockUpParasBean;
    @Override
    protected int getContentViewId() {
        return R.layout.activity_tock_up_manager;
    }

    @Override
    protected void processLogic() {
        binding.leoTitleBar.bar_left_btn.setBackgroundResource(R.mipmap.return_images);

        if(dialog==null) {
            dialog = CustomProgress.show(this, "", true, null);
        }
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(500);//休眠3秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                /**
                 * 要执行的操作
                 */
                dialog.dismiss();
            }
        }.start();
        binding.includeEmpty.setTextEmpty("瞅啥，没有你的物料信息~");
        binding.includeEmpty.setImageEmpty(R.mipmap.no_data);
        //

        stockUpParasBean=new StockUpParasBean();
        stockUpParasBean.setStockStatus(1);
        getStockUpList(stockUpParasBean);

        stockUpManagerAdapter=new StockUpManagerAdapter();
        stockUpManagerAdapter.setDataList(stockUpInfoBeans);
        binding.recyclerView.setAdapter(stockUpManagerAdapter);



        stockUpManagerAdapter.setOnclick(new StockUpManagerAdapter.ClickInterface() {
            @Override
            public void onImagesClick(View view, int position, StockUpResultBean materialInfoBean) {
                System.out.println("materialInfoBean"+materialInfoBean.toString());
                SharedPreferences sharedPreferences = getSharedPreferences("stockInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("orderId", materialInfoBean.getStockOrderNo()+"");
                editor.apply();

                Intent intent=new Intent(StockUpManagerActivity.this,StockUpActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemClick(View view, int position, StockUpResultBean stockUpInfoBean) {

            }
        });

    }
    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("返回过来刷新数据");
        stockUpInfoBeans.clear();
        getStockUpList(stockUpParasBean);

    }


    @Override
    protected void setListener() {
        binding.leoTitleBar.bar_left_btn.setOnClickListener(this);
    }
    private void getStockUpList(StockUpParasBean stockUpParasBean){
     mViewModel.getStockUpList(stockUpParasBean, null).observe(this, resourse->resourse.handler(new OnCallback<List<StockUpResultBean>>() {
         @Override
         public void onSuccess(List<StockUpResultBean> data) {
             if (data.size() == 0) {
                 binding.includeEmpty.relativeNull.setVisibility(View.VISIBLE);
             } else {
                 binding.includeEmpty.relativeNull.setVisibility(View.GONE);
                 stockUpInfoBeans.addAll(data);
             }
             stockUpManagerAdapter.notifyDataSetChanged();
             System.out.println("获取数据" + data.toString());
         }
     }));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bar_left_btn:
                this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
